﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Micrhard.FMCG.Server.Models;
using Micrhard.FMCG.Server.Msg;
using Micrhard.FMCG.Server.Interface;
using Microhard.Core;
using FluentValidation.Results;

namespace Micrhard.FMCG.Server.Validator
{
    public class DailyModelValidator : AbstractValidator<DailyModel>
    {
        public DailyModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Question).NotEmpty().WithMessage(DailyMsg.NeedQuestion).When(r => r.DailyID == Guid.Empty);
                RuleFor(u => u.Question).Length(1, 50).WithMessage(DailyMsg.MaxQuestionLength).When(r => r.DailyID == Guid.Empty);
                RuleFor(u => u.TodayPlan).NotEmpty().WithMessage(DailyMsg.NeedTodayPlan).When(r => r.DailyID == Guid.Empty);
                RuleFor(u => u.TodayPlan).Length(1, 500).WithMessage(DailyMsg.MaxTodayPlanLength).When(r => r.DailyID == Guid.Empty);
                RuleFor(u => u.TodayFinished).NotEmpty().WithMessage(DailyMsg.NeedTodayFinished).When(r => r.DailyID == Guid.Empty);
                RuleFor(u => u.TodayFinished).Length(1, 500).WithMessage(DailyMsg.MaxTodayFinishedLength).When(r => r.DailyID == Guid.Empty);
                RuleFor(u => u.Help).Length(1, 500).WithMessage(DailyMsg.MaxHelpLength).When(r => r.DailyID == Guid.Empty);

                Custom(u =>
                {
                    IDaily daily = IoC.Resolve<IDaily>();
                    if (daily.IsDuplicate(u))
                    {
                        return new ValidationFailure("CreateTime", DailyMsg.DulicateDaily);
                    }
                    return null;
                });
            });

            RuleSet("Update", () =>
            {
                RuleFor(u => u.Question).NotEmpty().WithMessage(DailyMsg.NeedQuestion);
                RuleFor(u => u.Question).Length(1, 50).WithMessage(DailyMsg.MaxQuestionLength);
                RuleFor(u => u.TodayPlan).NotEmpty().WithMessage(DailyMsg.NeedTodayPlan);
                RuleFor(u => u.TodayPlan).Length(1, 50).WithMessage(DailyMsg.MaxTodayPlanLength);
                RuleFor(u => u.TodayFinished).NotEmpty().WithMessage(DailyMsg.NeedTodayFinished);
                RuleFor(u => u.TodayFinished).Length(1, 50).WithMessage(DailyMsg.MaxTodayFinishedLength);
                RuleFor(u => u.Help).Length(1, 500).WithMessage(DailyMsg.MaxHelpLength);

                Custom(u =>
                {
                    IDaily daily = IoC.Resolve<IDaily>();
                    if (daily.IsApproved(u.DailyID))
                    {
                        return new ValidationFailure("CreateTime", DailyMsg.HasApproved);
                    }
                    return null;
                });
            });

            RuleSet("Approve", () =>
            {
                RuleFor(u => u.ApproveAdervise).NotEmpty().WithMessage(DailyMsg.NeedApprove);
                RuleFor(u => u.ApproveAdervise).Length(1, 250).WithMessage(DailyMsg.ApproveMaxLength);
            });
        }
    }
}
